package com.yjd.back.mns;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.credit.model.CreditLineApplyModel;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.PagerUtil;
import com.yjd.comm.util.ServletUtil;
import com.yjd.msg.comm.enums.MsgOrderServiceEnum;
import com.yjd.msg.comm.enums.MsgStatusEnum;
import com.yjd.msg.comm.enums.MsgTypeEnum;
import com.yjd.msg.comm.model.MsgDisposeLogModel;
import com.yjd.msg.comm.model.MsgInfoHistoryModel;
import com.yjd.msg.comm.model.MsgInfoModel;
import com.yjd.msg.comm.service.IMsgDisposeLogService;
import com.yjd.msg.comm.service.IMsgInfoHistoryService;
import com.yjd.msg.comm.service.IMsgInfoService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 消息控制器
 * Created by yjd-wap on 2018/1/27.
 */
@Controller
@Scope("prototype")
@RequestMapping("/msgInfo/")
public class MsgInfoControl extends BaseControl {


    @Resource
    private  IMsgInfoService msgInfoService;

    @Resource
    private IMsgDisposeLogService msgDisposeLogService;

    @Resource
    private  IMsgInfoHistoryService msgInfoHistoryService;

    /**
     * 消息待处理列表
     * @return void
     * @throws
     */
    @RequestMapping(value = "pending/list")
    public String pendingList() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        Map<String, Object> pageMap = paramMap;
        if("999".equals(paramMap.get("status"))){
            paramMap.put("status","");
        }
        paramMap.put("sort", "create_time desc");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.msgInfoService.getPageModel(paramMap, DbWREnums.READ), pageMap,
                ServletUtil.getUrl("/msgInfo/pending/list"));

        List<MsgInfoModel> msgInfoModels = (List<MsgInfoModel>) pager.getPagerModel().getDataList();

        for (MsgInfoModel model : msgInfoModels){
            MsgTypeEnum msgTypeEnum = MsgTypeEnum.getMsgTypeEnum(model.getMsg_type());
            model.getStrMap().put("source", MsgOrderServiceEnum.getOrderServiceDesc(model.getMsg_source()));
            model.getStrMap().put("create_time", DateUtil.timestampToDateString(model.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            model.getStrMap().put("update_time",DateUtil.timestampToDateString(model.getUpdate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            model.getStrMap().put("next_time",DateUtil.timestampToDateString(model.getNext_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            model.getStrMap().put("status", MsgStatusEnum.getMsgStatusDesc(model.getStatus()));
            model.getStrMap().put("msg_type", msgTypeEnum==null?model.getMsg_type():msgTypeEnum.getDesc());
        }
        request.setAttribute("typeList", MsgTypeEnum.getMsgTypeList());
        request.setAttribute("pager", pager);
        return "msgInfo.pending.list";
    }

    /**
     * 历史消息列表
     * @return void
     * @throws
     */
    @RequestMapping(value = "history/list")
    public String historyList() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("sort", "create_time desc");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.msgInfoHistoryService.getPageModel(paramMap, DbWREnums.READ), paramMap,
                ServletUtil.getUrl("/msgInfo/history/list"));

        List<MsgInfoHistoryModel> historyModels = (List<MsgInfoHistoryModel>) pager.getPagerModel().getDataList();
        for (MsgInfoHistoryModel model : historyModels){
            MsgTypeEnum msgTypeEnum = MsgTypeEnum.getMsgTypeEnum(model.getMsg_type());

            model.getStrMap().put("source", MsgOrderServiceEnum.getOrderServiceDesc(model.getMsg_source()));
            model.getStrMap().put("create_time", DateUtil.timestampToDateString(model.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            model.getStrMap().put("update_time",DateUtil.timestampToDateString(model.getUpdate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            model.getStrMap().put("status", MsgStatusEnum.getMsgStatusDesc(model.getStatus()));
            model.getStrMap().put("msg_type", msgTypeEnum==null?model.getMsg_type():msgTypeEnum.getDesc());
        }
        request.setAttribute("typeList", MsgTypeEnum.getMsgTypeList());
        request.setAttribute("pager", pager);
        return "msgInfo.history.list";
    }

    @RequestMapping(value = "disposeDetail")
    public String disposeDetail(Long msg_id)throws Exception{
        if(msg_id==null){
            FrameUtil.throwBaseException1("请求参数错误");
        }
        List<MsgDisposeLogModel> logModels = msgDisposeLogService.getModelList(FrameUtil.newHashMap("msg_id",msg_id),DbWREnums.READ);
        for(MsgDisposeLogModel model :logModels){
            model.getStrMap().put("dispose_time",DateUtil.timestampToDateString(model.getDispose_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            model.getStrMap().put("result", MsgStatusEnum.getMsgStatusDesc(model.getResult()));
            model.getStrMap().put("msg_type", MsgTypeEnum.getMsgTypeEnum(model.getMsg_type()).getDesc());
        }
        request.setAttribute("logModels",logModels);
        return "msgInfo.dispose.detail";
    }

    @RequestMapping(value = "dispose/{msg_id}")
    public void dispose(@PathVariable("msg_id") Long msg_id)throws Exception{
        if(msg_id==null){
            FrameUtil.throwBaseException1("请求参数错误");
        }
        this.msgInfoService.msgBusinessDispose(msg_id);
        this.successMsg("操作成功");
    }
}
